What is replace?
The 'replace' npm package is a utility for performing search and replace operations on files. It allows you to replace text in files using a simple API, making it useful for tasks such as updating configuration files, modifying source code, or performing batch text replacements.
What are replace's main functionalities?
Basic Text Replacement
This feature allows you to replace occurrences of a specified string (in this case, 'foo') with another string ('bar') in the specified file ('file.txt'). The 'recursive' option allows the operation to be performed in all files within the specified directory, and 'silent' suppresses the output.
const replace = require('replace');
replace({
regex: 'foo',
replacement: 'bar',
paths: ['file.txt'],
recursive: true,
silent: true
});
Regular Expression Replacement
This feature allows you to use regular expressions for more complex search patterns. In this example, any string matching the pattern 'foo' followed by one or more digits will be replaced with 'bar'.
const replace = require('replace');
replace({
regex: /foo\d+/g,
replacement: 'bar',
paths: ['file.txt'],
recursive: true,
silent: true
});
Replacement with Function
This feature allows you to use a function to determine the replacement text. In this example, any occurrence of 'foo' will be replaced with its uppercase version.
const replace = require('replace');
replace({
regex: 'foo',
replacement: (match) => match.toUpperCase(),
paths: ['file.txt'],
recursive: true,
silent: true
});
Other packages similar to replace
replace-in-file
The 'replace-in-file' package provides similar functionality to 'replace' but with a more modern API and additional features such as dry-run mode and custom encoding support. It is often preferred for its ease of use and additional options.
string-replace-loader
The 'string-replace-loader' package is a webpack loader that allows you to perform string replacements in your source files during the build process. It is useful for build-time replacements and integrates seamlessly with webpack.
gulp-replace
The 'gulp-replace' package is a plugin for the Gulp task runner that allows you to perform string replacements in your Gulp pipelines. It is ideal for automating text replacements as part of your build and deployment workflows.
COMMON ISSUE ON WINDOWS
There is a built-in replace command on windows, if you get an error like this
Invalid switch - -h
No files replaced
You are using the windows replace
replace
replace
is a command line utility for performing search-and-replace on files. It's similar to sed but there are a few differences:
Install
With node.js and npm:
npm install replace -g
You can now use replace
and search
from the command line.
Examples
Replace all occurrences of "foo" with "bar" in files in the current directory:
replace 'foo' 'bar' *
Replace in all files in a recursive search of the current directory:
replace 'foo' 'bar' . -r
Replace only in test/file1.js and test/file2.js:
replace 'foo' 'bar' test/file1.js test/file2.js
Replace all word pairs with "_" in middle with a "-":
replace '(\w+)_(\w+)' '$1-$2' *
Replace only in files with names matching *.js:
replace 'foo' 'bar' . -r --include="*.js"
Don't replace in files with names matching *.min.js and *.py:
replace 'foo' 'bar' . -r --exclude="*.min.js,*.py"
Preview the replacements without modifying any files:
replace 'foo' 'bar' . -r --preview
Replace using stdin:
echo "asd" | replace "asd" "dsa" -z
See all the options:
replace -h
Search
There's also a search
command. It's like grep
, but with replace
's syntax.
search "setTimeout" . -r
Programmatic Usage
You can use replace from your JS program:
var replace = require("replace");
replace({
regex: "foo",
replacement: "bar",
paths: ['./Test/'],
recursive: false,
silent: false,
});
More Details
Excludes
By default, replace
and search
will exclude files (binaries, images, etc) that match patterns in the "defaultignore"
located in this directory.
On huge directories
If replace
is taking too long on a large directory, try turning on the quiet flag with -q
, only including the necessary file types with --include
or limiting the lines shown in a preview with -n
.
What it looks like